package com.erp.module.jiJian

import android.app.Activity
import androidx.appcompat.app.AlertDialog
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.erp.R
import com.erp.bean.JijianBean
import com.erp.bean.Twostringbean
import com.erp.module.jiJian.shangChuanJiJian.ShuangChuanJiJianKActivity
import com.erp.myAdapter.JijianhuizongAdapter
import com.erp.myFragment.BaseKFragment
import com.erp.permission.PermissionListener
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.util.PermissionUtils
import com.erp.view.PullToRefreshView
import kotlinx.android.synthetic.main.fragment_jijian.*
import org.json.JSONException
import org.json.JSONObject




/**
 * Created by Administrator on 2017/11/10/010.
 * 作者：刘洋
 * 计件界面
 */
class FragmentKJiJian : BaseKFragment(), JiJianContract.JiJianView, View.OnClickListener, PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {

    lateinit var P : IJiJianP
    lateinit var searchParams: ArrayList<Twostringbean>
    lateinit var adapter: JijianhuizongAdapter
    lateinit var jijianBeanList: MutableList<JijianBean>
    var updialog: AlertDialog? = null
    lateinit var dialog : Dialog
    private val f = "FragmentKJiJian"
    override fun getLayoutId(): Int {
        fragmentname=f
        return R.layout.fragment_jijian
    }

    override fun initData() {
        jijianBeanList = ArrayList()
        P= IJiJianP(this, activity!!,jijianBeanList)
        adapter = JijianhuizongAdapter(jijianBeanList, activity!!)
        JiJianLv.adapter=adapter
        JiJianFragmentPv.setheader(true)
        JiJianFragmentPv.setOnHeaderRefreshListener(this)
        JiJianFragmentPv.setOnFooterRefreshListener(this)
        searchParams = ArrayList()
        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.TIMELEI, "pw.start_time", searchParams)
        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.STARTTIME, Myutil.getjulidate1(0) + " 00:00", searchParams)
        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.ENDTIME, Myutil.getjulidate1(1) + " 00:00", searchParams)
        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.GONGXU, "0", searchParams)
        GuoLvTv.setOnClickListener(this)
        ScanLl.setOnClickListener(this)
        P.gainData("JiJian")
        P.sousuo(searchParams)
        JiJianFragmentPv.headerRefreshing()
    }
    override fun xialaOk() {
        updialog?.dismiss()
        JiJianFragmentPv.onHeaderRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun xialaNo() {
        updialog?.dismiss()
        JiJianFragmentPv.onHeaderRefreshComplete()
    }

    override fun shanglaOk() {
        updialog?.dismiss()
        JiJianFragmentPv.onFooterRefreshComplete()
        adapter.notifyDataSetChanged()
    }

    override fun shanglaNo() {
        updialog?.dismiss()
        JiJianFragmentPv.onFooterRefreshComplete()
    }
    override fun gainDataOk() {
        adapter.notifyDataSetChanged()
    }

    override fun onFooterRefresh(view: PullToRefreshView) {
        P.shangla()
    }
    override fun onHeaderRefresh(view: PullToRefreshView) {
        P.xiala()
    }
    override fun onClick(v: View?) {
        when(v){
            GuoLvTv -> {
                var intent = Intent()
                intent.setClass(activity!!, JiJianGuoLvActivity::class.java)
                val bundle = Bundle()
                bundle.putParcelableArrayList("searchParams", searchParams)
                intent.putExtra("searchParams", bundle)
                startActivityForResult(intent, 101)
                activity!!.overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
            }
            ScanLl -> {
                PermissionUtils.issaomiao(activity!!,object : PermissionListener {
                    override fun onSucceed(requestCode: Int, grantPermissions: MutableList<String>) {
                        var intent=Intent(activity, ShuangChuanJiJianKActivity:: class.java)
                        startActivityForResult(intent,102)
                    }

                    override fun onFailed(requestCode: Int, deniedPermissions: MutableList<String>) {
                        Myutil.MyTost("没有权限，无法扫描")
                    }

                })
            }
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null||resultCode!= Activity.RESULT_OK){
            return
        }
        when (requestCode) {
            101 ->  {
                val bundle = data.getParcelableExtra<Bundle>("searchParams")
                searchParams = bundle.getParcelableArrayList("searchParams")!!
                updialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(searchParams)
                P.xiala()
            }
            102 -> {
                val data=data.getStringExtra("data")
                Log.d("TAAAG","data="+Myutil.decodeUnicode(data))
                if(data==""){
                    return
                }
                searchParams.clear()
                try {
                    val jo = JSONObject(data)
                    Myutil.MyTost(JsonUtil.getvolue(jo, "msg"))
                    val jo1 = JsonUtil.getjovolue(jo, "searchParams")
                    if (jo1.has("time_filter")) {
                        val joTime = jo1.getJSONObject("time_filter")
                        if (joTime.has("time_category")) {
                            JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.TIMELEI, joTime.getString("time_category"), searchParams)
                        }
                        if (joTime.has("start_time")) {
                            JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.STARTTIME, Myutil.getdata(joTime.getString("start_time")), searchParams)
                        }
                        if (joTime.has("end_time")) {
                            JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.ENDTIME, Myutil.getdata(joTime.getString("end_time")), searchParams)
                        }
                    }
                    if (jo1.has("wu_name")) {
                        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.NAME, jo1.getString("wu_name"), searchParams)
                    }
                    if (jo1.has("work_number")) {
                        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.NUMBLE, jo1.getString("work_number"), searchParams)
                    }
                    if (jo1.has("pw_ws")) {
                        JiJianGuoLvActivity.addsearchParams(JiJianGuoLvActivity.GONGXU, jo1.getString("pw_ws"), searchParams)
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                updialog = Myutil.showupdatedialog(activity, "正在刷新", false)
                P.sousuo(searchParams)
                P.xiala()
            }
        }
    }
    fun qingkong(){
        if(isFirstLoad){
            return
        }
        JiJianFragmentPv.headerRefreshing()
    }

}